home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Ghost 1.0 / source / Ghost ƒ / Ghost code / ghost load-save.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  6.3 KB  |  247 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        ghost load-save.c
  4.  
  5. Purpose:    This module handles loading and saving games on disk.
  6.  
  7.  
  8. Ghost -=- a classic word-building challenge
  9. Copyright (C) 1993 Mark Pilgrim
  10.  
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program in a file named "GNU General Public License".
  23. If not, write to the Free Software Foundation, 675 Mass Ave,
  24. Cambridge, MA 02139, USA.
  25.  
  26. \**********************************************************************/
  27.  
  28. #include "ghost globals.h"
  29. #include "ghost error.h"
  30. #include "ghost load-save.h"
  31. #include "ghost files.h"
  32. #include "ghost.h"
  33. #include "msg dialogs.h"
  34. #include "msg environment.h"
  35. #include "util.h"
  36. #include "Script.h"
  37.  
  38. FSSpec            gameFile;
  39. Boolean            deleteTheThing;
  40.  
  41. typedef struct
  42. {
  43.     unsigned int    version;                    /* for forward compatibility */
  44.     int                numComputerPlayers;
  45.     int                computerIconIndex[5];
  46.     int                numHumanPlayers;
  47.     unsigned char    humanName[5][40];
  48.     int                humanIconIndex[5];
  49.     int                actualHumanPlayers;
  50.     int                actualComputerPlayers;
  51.     int                numPlayers;
  52.     int                playOrderIndex[10];
  53.     int                computerPlayerScore[5];
  54.     int                humanPlayerScore[5];
  55.     unsigned char    theWord[20];
  56.     int                currentPlayer;
  57.     unsigned char    showMessageBox;
  58.     unsigned char    gameSpeed;
  59.     unsigned char    computerIntelligence;
  60.     unsigned char    useFullDictionary;
  61.     unsigned char    checksum;                    /* checksum of previous data */
  62. } SaveStruct;
  63.  
  64. void InitLoadSave(void)
  65. {
  66.     gameFile.name[0]=0x00;
  67.     deleteTheThing=FALSE;
  68. }
  69.  
  70. void LoadSaveDispatch(Boolean isLoad, Boolean useOldGame)
  71. {
  72.     if (isLoad)
  73.     {
  74.         if (GetSourceFile(&gameFile, useOldGame))
  75.             HandleError(GetSavedGame(&gameFile));
  76.     }
  77.     else
  78.     {
  79.         useOldGame=useOldGame&&(gameFile.name[0]!=0x00);
  80.         if (useOldGame ? TRUE : GetDestFile(&gameFile, &deleteTheThing))
  81.             HandleError(SaveGame(gameFile));
  82.     }
  83. }
  84.  
  85. int SaveGame(FSSpec gameFile)
  86. {
  87.     OSErr            theError;
  88.     int                thisFile;
  89.     long            count;
  90.     SaveStruct        data;
  91.     unsigned char    checksum;
  92.     int                i,j;
  93.     
  94.     data.version=SAVE_VERSION;
  95.     data.numComputerPlayers=gNumComputerPlayers;
  96.     for (i=0; i<5; i++)
  97.         data.computerIconIndex[i]=gComputerIconIndex[i];
  98.     data.numHumanPlayers=gNumHumanPlayers;
  99.     for (i=0; i<gNumHumanPlayers; i++)
  100.         BlockMove(*gHumanName[i], data.humanName[i], 40);
  101.     for (i=0; i<5; i++)
  102.         data.humanIconIndex[i]=gHumanIconIndex[i];
  103.     data.actualHumanPlayers=gActualHumanPlayers;
  104.     data.actualComputerPlayers=gActualComputerPlayers;
  105.     data.numPlayers=gNumPlayers;
  106.     for (i=0; i<10; i++)
  107.         data.playOrderIndex[i]=gPlayOrderIndex[i];
  108.     for (i=0; i<5; i++)
  109.         data.computerPlayerScore[i]=gComputerPlayerScore[i];
  110.     for (i=0; i<5; i++)
  111.         data.humanPlayerScore[i]=gHumanPlayerScore[i];
  112.     BlockMove(gTheWord, data.theWord, 20);
  113.     data.currentPlayer=gCurrentPlayer;
  114.     data.showMessageBox=gShowMessageBox;
  115.     data.gameSpeed=gGameSpeed;
  116.     data.computerIntelligence=gComputerIntelligence;
  117.     data.useFullDictionary=gUseFullDictionary;
  118.     
  119.     checksum=0;
  120.     for (i=0; i<sizeof(SaveStruct)-2; i++)
  121.         checksum+=*((unsigned char*)((long)&data+i));
  122.     data.checksum=checksum;
  123.     
  124.     if (deleteTheThing)
  125.     {
  126.         if (gHasFSSpecs)
  127.             FSpDelete(&gameFile);
  128.         else
  129.             HDelete(gameFile.vRefNum, gameFile.parID, gameFile.name);
  130.     }
  131.     FlushVol(0L, gameFile.vRefNum);
  132.     
  133.     if (gHasFSSpecs)
  134.         theError=FSpCreate(&gameFile, CREATOR, SAVE_TYPE, smSystemScript);
  135.     else
  136.         theError=HCreate(gameFile.vRefNum, gameFile.parID,
  137.                         gameFile.name, CREATOR, SAVE_TYPE);
  138.     FlushVol(0L, gameFile.vRefNum);
  139.     
  140.     if (theError!=noErr)
  141.         return kCantCreateGame;
  142.     
  143.     if (gHasFSSpecs)
  144.         theError=FSpOpenDF(&gameFile, fsRdWrPerm, &thisFile);
  145.     else
  146.         theError=HOpen(gameFile.vRefNum, gameFile.parID,
  147.                         gameFile.name, fsRdWrPerm, &thisFile);
  148.     
  149.     if (theError!=noErr)
  150.         return kCantOpenGameToSave;
  151.         
  152.     count=sizeof(SaveStruct);
  153.     SetEOF(thisFile, count);
  154.     SetFPos(thisFile, 1, 0L);
  155.     theError=FSWrite(thisFile, &count, &data);
  156.     FSClose(thisFile);
  157.     FlushVol(0L, gameFile.vRefNum);
  158.  
  159.     if (theError!=noErr)
  160.     {
  161.         if (gHasFSSpecs)
  162.             FSpDelete(&gameFile);
  163.         else
  164.             HDelete(gameFile.vRefNum, gameFile.parID, gameFile.name);
  165.         gameFile.name[0]=0x00;
  166.         return kCantWriteGame;
  167.     }
  168.     else deleteTheThing=TRUE;
  169.     
  170.     return allsWell;
  171. }
  172.  
  173. int GetSavedGame(FSSpec *gameFile)
  174. {
  175.     int                thisFile;
  176.     unsigned char    checksum;
  177.     long            count;
  178.     int                i,j;
  179.     SaveStruct        data;
  180.     OSErr            theError;
  181.  
  182.     if (gHasFSSpecs)
  183.         theError=FSpOpenDF(gameFile, fsRdPerm, &thisFile);
  184.     else
  185.         theError=HOpen(gameFile->vRefNum, gameFile->parID, gameFile->name, fsRdPerm, &thisFile);
  186.     
  187.     if (theError!=noErr)
  188.         return kCantOpenGameToLoad;
  189.     
  190.     count=sizeof(SaveStruct);
  191.     SetFPos(thisFile, 1, 0L);
  192.     theError=FSRead(thisFile, &count, &data);
  193.     FSClose(thisFile);
  194.  
  195.     if (theError!=noErr)
  196.         return kCantLoadGame;
  197.     
  198.     deleteTheThing=TRUE;
  199.     
  200.     if (data.version!=SAVE_VERSION)
  201.         return kSaveVersionNotSupported;
  202.     
  203.     checksum=0;
  204.     for (i=0; i<sizeof(SaveStruct)-2; i++)
  205.             checksum+=*((unsigned char*)((long)&data+i));
  206.  
  207.     if (checksum!=data.checksum)
  208.     {
  209.         gameFile->name[0]=0x00;
  210.         return kBadChecksum;
  211.     }
  212.  
  213.     gNumComputerPlayers=data.numComputerPlayers;
  214.     for (i=0; i<5; i++)
  215.         gComputerIconIndex[i]=data.computerIconIndex[i];
  216.     gNumHumanPlayers=data.numHumanPlayers;
  217.     for (i=0; i<gNumHumanPlayers; i++)
  218.     {
  219.         if (gHumanName[i]!=0L)
  220.             DisposeHandle(gHumanName[i]);
  221.         gHumanName[i]=NewString(data.humanName[i]);
  222.     }
  223.     for (i=0; i<5; i++)
  224.         gHumanIconIndex[i]=data.humanIconIndex[i];
  225.     gActualHumanPlayers=data.actualHumanPlayers;
  226.     gActualComputerPlayers=data.actualComputerPlayers;
  227.     gNumPlayers=data.numPlayers;
  228.     for (i=0; i<10; i++)
  229.         gPlayOrderIndex[i]=data.playOrderIndex[i];
  230.     for (i=0; i<5; i++)
  231.         gComputerPlayerScore[i]=data.computerPlayerScore[i];
  232.     for (i=0; i<5; i++)
  233.         gHumanPlayerScore[i]=data.humanPlayerScore[i];
  234.     BlockMove(data.theWord, gTheWord, 20);
  235.     gCurrentPlayer=data.currentPlayer;
  236.     gShowMessageBox=data.showMessageBox;
  237.     gGameSpeed=data.gameSpeed;
  238.     gComputerIntelligence=data.computerIntelligence;
  239.     gUseFullDictionary=data.useFullDictionary;
  240.     
  241.     gInProgress=TRUE;
  242.     gCurrentPlayer--;
  243.     StartGame();
  244.     
  245.     return allsWell;
  246. }
  247.